Tack.Designer = new Class({
	initialize: function (tack) {
		this.tack = tack;

		this.html = {
			main: new Element('div', { 'class': 'scene' })
		};

		this.actions = {
			'End game': Tack.Action.EndGame,
			'Set state': Tack.Action.SetState,
			'Show text': Tack.Action.ShowText,
			'Change scene': Tack.Action.ChangeScene
		};

		this.conditions = {
			'Event argument is': Tack.Condition.EventArgument,
			'Previous scene was': Tack.Condition.PreviousScene,
			'State on?': Tack.Condition.StateOn
		};

		this.events = {
			'Entering scene': 'entering',
			'Command entered': 'command'
		};

		this.actionMenu = new Tack.Menu({
			'Remove': function () { this.remove(); }
		});

		var items = {
			'New action': {},
			'New condition': {},
			'Add else-block': function () { this.newElseBlock(); this.html.main.removeClass('collapsed'); },
			'Add action block': function () { this.newAction(Tack.ActionBlock); this.html.main.removeClass('collapsed'); },
			'Collapse/Expand': function () { this.html.main.toggleClass('collapsed'); },
			'Remove': function () { this.remove(); }
		};

		$each(this.actions, function (type, name) {
			items['New action'][name] = function () { this.newAction(type); this.html.main.removeClass('collapsed'); };
		}, this);

		$each(this.conditions, function (type, name) {
			items['New condition'][name] = function () { this.newCondition(type); this.html.main.removeClass('collapsed'); };
		}, this);

		this.actionBlockMenu = new Tack.Menu(items);

		this.conditionMenu = new Tack.Menu({
			'Toggle negation': function () { this.toggleNegation(); },
			'Remove': function () { this.remove(); }
		});

		tack.html.main.adopt(this.html.main);
	},

	refresh: function () {
		if (!this.scene instanceof Tack.Scene || !this.tack.game.scenes.hasValue(this.scene)) {
			this.html.main.empty();
			return;
		}
	
		var ul_events = new Element('ul', { 'class': 'events' });
		$each(this.events, function (event, name) {
			var li_event = new Element('li', { 'class': 'event' });

			li_event.adopt(new Element('span', { 'class': 'event-title', text: name }));

			var ol_actions = new Element('ol', { 'class': 'actions' });
			ol_actions.adopt(new Tack.Designer.ActionBlock(this, this.scene.events[event]).html.main);
			li_event.adopt(ol_actions);

			ul_events.adopt(li_event);
		}, this);

		this.html.main.empty();
		this.html.main.adopt(new Element('h2', { text: this.scene.name }));
		this.html.main.adopt(ul_events);
	},

	setScene: function (scene) {
		if (this.scene == scene) return;

		this.scene = scene;
		this.tack.refresh();
	}
});
